package tema8.ej04;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class Main {
	private static Session session;

	private static void startSession() {
		Configuration conf = new Configuration().configure();
		ServiceRegistry sr = new StandardServiceRegistryBuilder()
				.applySettings(conf.getProperties()).build();
		SessionFactory sf=conf.buildSessionFactory(sr);
		session=sf.openSession();
	}
	
	private static void guardarAmo(Amo amo) {
		Transaction t = session.beginTransaction();
		session.persist(amo);
		t.commit();
	}
	public static void main(String[] args) {
		startSession();
		//crearManoloPupi();
		//borrarManolo();
		//borrarPupi();
	}

	
	@SuppressWarnings("unused")
	private static void borrarManolo() {
		Transaction t = session.beginTransaction();
		Amo manolo=(Amo)session.load(Amo.class, 1L);
		t.commit();
		
		t = session.beginTransaction();
		session.delete(manolo);
		t.commit();
	}
	
	
	@SuppressWarnings("unused")
	private static void borrarPupi() {
		Transaction t = session.beginTransaction();
		Mascota pupi=(Mascota)session.load(Mascota.class, 1L);
		t.commit();
		
		t = session.beginTransaction();
		pupi.getAmo().setMascota(null);
		pupi.setAmo(null);
		session.delete(pupi);
		t.commit();
	}


	@SuppressWarnings("unused")
	private static void crearManoloPupi() {
		Amo manolo =new Amo("Manolo");
		Mascota pupi=new Mascota("pupi");

		manolo.setMascota(pupi);
		pupi.setAmo(manolo);

		guardarAmo(manolo);
	}

}
